package com.symbio.ishenapi.background.controller;


import com.symbio.ishenapi.background.bean.AttentionDoctorBean;
import com.symbio.ishenapi.background.model.TmDoctorUser;
import com.symbio.ishenapi.background.model.TmPatientDoctor;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/P_D")
public class BoundPatientDoctor extends BaseController {

    public void BindingPD(){    //绑定医生
        TmPatientDoctor tpd = (TmPatientDoctor) getReqBodyObject(TmPatientDoctor.class);
        if("".equals(tpd.getUserId()) || tpd.getUserId()==null){

            renderJsonError(40003,"userId");
            return;
        }else if("".equals(tpd.getDoctorId()) || tpd.getDoctorId()==null){
            renderJsonError(40003,"doctorId");
            return;
        }
        int num=TmPatientDoctor.dao.getUserCountById(tpd);
        if(num>=1){
            renderJsonError(40011,"该用户与所绑定医生");
            return;
        }
        tpd.set("createTime", new Date());
        if(tpd.save()){///绑定成功返回成功信息
            renderJsonRet(tpd.getDoctorId());
        }else{////绑定失败
            renderJsonError(40002,"关注医生信息");
        }
    }

    public void findP_D(){      //查看关联医生
        System.out.println("进来");
        //renderText("abc");
        BigInteger userId = new BigInteger(getPara("userId"), 10);
        System.out.println("获取到的用户ID为"+userId);
        List<AttentionDoctorBean> result = TmPatientDoctor.dao.findTmPatientDoctorList(userId);
        renderJson(result.get(0));
    }

    /**
     *wy:关注医生管理
     *  批量删除医生
     */
    public void delBatchDoctor(){
       String ids = getReqBodyString().replace("\"","").replace("\"","");
       System.out.println(ids);
       if (TmPatientDoctor.dao.delBatchDoctor(ids)) {
           renderJsonRet(500001);
       }else {
           renderJsonError(500002,"禁用");
       }
    }

    /**
     *关注医生管理
     *删除医生
     */
    public void delDoctor(){
        TmPatientDoctor user = (TmPatientDoctor) getReqBodyObject(TmPatientDoctor.class);
        if (user.update()){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002,"修改");
        }
    }

    /**
     *wy:关注医生管理
     *显示医生列表
     */
    public void findDoctorList(){
        String ids = getPara("id");
        String userMedical = getPara("userMedical");
        if (ids != null && !"".equals(ids)){
            BigInteger id = new BigInteger(ids);
            Map map = TmPatientDoctor.dao.findDoctorList(userMedical,id,getQueryModel());
            if (null != map && map.size() > 0){
                renderPageJsonRet(map.get("count"),map.get("data"));
            }else {
                renderJsonError(40003,"请重试!");
            }
        }else {
            renderJsonError(40003,"请重试!");
        }
    }

    /**
     * wy:查询医生类型   0医生  1医疗团
     */
    public void findDoctorMedical(){
        String userMedical = getPara("userMedical");
        Map map = TmDoctorUser.dao.findDoctorMedical(getQueryModel(),userMedical);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * wy:关注医生管理
     *     患者医生关联
     */
    public void saveGzDoctor(){
        TmPatientDoctor tpd = (TmPatientDoctor) getReqBodyObject(TmPatientDoctor.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        tpd.setCreateTime(sdf.format(new Date()));
        if (tpd.save()){
            renderJsonRet(500001);
        } else {
            renderJsonError(500002,"关注添加");
        }
    }
}
